﻿<style>
    .layui-table-cell {
        height: 55px;
        font-size: 0.88em;
    }

    .layui-table td img {
        width: 55px;
    }

    .fontview {
        font-weight: 600;
        color: #3384b2;
        text-decoration: underline;
        cursor: pointer;
    }
    .bksp{
        color:white;
        background:#FF5722;
        border:2px solid #FF5722;
        border-radius:3px;
        cursor:pointer;
    }
    .jgsp{
        color:#c2c2c2;
        font-weight:bold;
        cursor:pointer;
    }
    .font1 {
        font-weight: 600;
    }

    .font2 {
        color: black;
    }

    .font3 {
        color: #9b9b9b;
    }

    .font4 {
        background: black;
        font-size: 0.8em !important;
    }
    .icon1{
        color:#FF5722;
        font-size:14px;
        cursor:pointer;
    }
    .orders{
        cursor:pointer;
    }
    
    .xm-select-dl{
        width:460px !important;
    }
    .xm-select-linkage .xm-select-linkage-group li {
        height:24px;
        line-height:24px
    }
    .xm-select-linkage .xm-select-linkage-group li span{
       line-height:24px
    }
</style>
<script type="text/html" template>
    <title>{{layui.setter.MenuName}}-{{layui.setter.name}}</title>
</script>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <script type="text/html" template>
            <a><cite>{{layui.setter.MenuName}}</cite></a>
        </script>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">关键字</label>
                    <div class="layui-input-inline">
                        <input type="text" name="key" id="key" placeholder="名称/编码/品牌/关键字" autocomplete="off" class="layui-input">
                    </div>
                    <label class="layui-form-label">商品状态</label>
                    <div class="layui-input-inline">
                        <select name='show' id='show' lay-search>
                            <option value=""></option>
                            <option value="0">上架</option>
                            <option value="1">下架</option>
                        </select>
                    </div>
                    <label class="layui-form-label">分类</label>
                    <div class="layui-input-inline" style="width:240px;">
                        <select name="cateID" xm-select="cateID" xm-select-radio="" xm-select-search="" xm-select-skin="default">
                            <option value=""></option>
                        </select>
                    </div>
                    <!--<input type="checkbox" name="wpick" lay-skin="switch" lay-filter="switchTest2" lay-text="网点自提|网点自提">-->
                </div>
                <div class="layui-inline">
                    <!--<label class="layui-form-label">配送方式</label>
                    <div class="layui-input-inline">
                        <select name='dMode' xm-select="dMode" id='dMode'>
                            <option value=""></option>
                        </select>
                    </div>-->
                    <!--<label class="layui-form-label">店铺</label>
                    <div class="layui-input-inline">
                        <select name='storeID' id='storeID' lay-search>
                            <option value=""></option>
                        </select>
                    </div>-->
                    <!--<label class='layui-form-label'>店铺</label>
                    <div class='layui-input-inline'>
                        <select name="storeID" xm-select="storeID" xm-select-radio="" xm-select-search xm-select-skin="default">
                            <option value=""></option>
                        </select>
                    </div>-->
                    <!--<label class="layui-form-label">供应商</label>
                    <div class="layui-input-inline" style="width:240px;">
                        <select name='supplier' id='supplier' lay-search>
                            <option value=""></option>
                        </select>
                    </div>-->
                    <!--<label class='layui-form-label'>供应商</label>
                    <div class='layui-input-inline' style="width:240px;">
                        <select name="selsupplier" xm-select="selsupplier" xm-select-radio="" xm-select-search xm-select-skin="default">
                            <option value=""></option>
                        </select>
                        <input type="hidden" name="supplier" id="supplier">
                    </div>-->
                    <input type="checkbox" name="ship" lay-skin="switch" lay-filter="switchTest" lay-text="包邮|包邮">
                    <!--<input type="checkbox" name="groupbuy" lay-skin="switch" lay-filter="switchTest3" lay-text="团购|团购">-->
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-delete">
                        <i class="layui-icon layui-icon-delete layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <script type="text/html" template lay-url="{{ layui.setter.APIUrl }}/shop/goodsMgr/GetButtonRight" lay-type="post" lay-done="layui.data.setActive()">
                    {{ d.Data }}
                </script>
            </div>
            <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
        </div>
    </div>
</div>
<script>
    var checkids = [];  //商品ID集合

    var form, admin, table, view, formSelects, $;
    layui.use('shop_goods', layui.factory('shop_goods')).use(['admin', 'table', 'form', 'formSelects'], function () {

        $ = layui.$;
        admin = layui.admin;
        view = layui.view;
        table = layui.table;
        form = layui.form;
        formSelects = layui.formSelects;

        form.render(null, 'app-content-list');

        formSelects.config('cateID', {
            keyName: 'title',            //自定义返回数据中name的key, 默认 name
            keyVal: 'id',            //自定义返回数据中value的key, 默认 value
            keyChildren: 'children',    //联动多选自定义children
        });

        admin.req({
            url: layui.setter.APIUrl + '/shop/GoodsCategoryMgr/customerQueryChildren', //实际使用请改成服务端真实接口
            type: 'post',
            done: function (res) {
                layui.formSelects.data('cateID', 'local', {
                    arr: res.Data,
                });
            }
        });
        //这里也算是给自己挖了一手好坑吧, 简单的一种处理方式, 在多选第一次打开的时候收缩所有的子节点, 目前处理的很粗鲁, 就是为了一种效果, 收缩全部节点
        var isFirst = true;
        layui.formSelects.opened('cateID', function (id) {
            if (isFirst) {
                isFirst = false;
                $('[fs_id="cateID"]').find('.xm-cz i.icon-caidan').click();
            }
        });

        formSelects.config('dMode', {
            keyName: 'Text',            //自定义返回数据中name的key, 默认 name
            keyVal: 'PID',            //自定义返回数据中value的key, 默认 value
        });
        layui.formSelects.data('dMode',
            'local',
            {
                arr: [{ 'Text': '自提', 'PID': '0' }, { 'Text': '快递', 'PID': '1' }],//要把自己的这个节点去掉
            });

        //admin.req({
        //    url: layui.setter.APIUrl + '/shop/StoreMgr/customerQuery', //实际使用请改成服务端真实接口
        //    type: 'post',
        //    done: function (res) {
        //        var html = "<option value=''></option>";
        //        layui.each(res.Data, function (index, eachdata) {
        //            html += "<option value='" + eachdata.PID + "'>" + eachdata.Name + "</option>";
        //        });
        //        $("#storeID").html(html);
        //        form.render('select');
        //    }
        //});

        formSelects.config('storeID', {
            keyName: 'title',            //自定义返回数据中name的key, 默认 name
            keyVal: 'id',            //自定义返回数据中value的key, 默认 value
            keyChildren: 'children',    //联动多选自定义children
        });

        admin.req({
            url: layui.setter.APIUrl + '/shop/storeMgr/customerQueryChildren2', //实际使用请改成服务端真实接口
            type: 'post',
            done: function (res) {
                layui.formSelects.data('storeID', 'local', {
                    arr: res.Data,
                    linkage: true,
                    linkageWidth: 120
                });
            }
        });
        formSelects.on('storeID', function (id, vals, val, isAdd, isDisabled) {
            if (isAdd == true) {
                if (val.value.indexOf("/") <= 0) {  //多级选择时,第一级无法选中
                    isAdd = false;
                    return false;
                }
            }
        });

        //admin.req({
        //    url: layui.setter.APIUrl + '/shop/SupplierMgr/customerQuery', //实际使用请改成服务端真实接口
        //    type: 'post',
        //    done: function (res) {
        //        var html = "<option value=''></option>";
        //        layui.each(res.Data, function (index, eachdata) {
        //            html += "<option value='" + eachdata.PID + "'>" + eachdata.Name + "</option>";
        //        });
        //        $("#supplier").html(html);
        //        form.render('select');
        //    }
        //});

        formSelects.config('selsupplier', {
            keyName: 'title',            //自定义返回数据中name的key, 默认 name
            keyVal: 'id',            //自定义返回数据中value的key, 默认 value
            keyChildren: 'children',    //联动多选自定义children
        });

        admin.req({
            url: layui.setter.APIUrl + '/shop/supplierMgr/customerQueryChildren2', //实际使用请改成服务端真实接口
            type: 'post',
            done: function (res) {
                layui.formSelects.data('selsupplier', 'local', {
                    arr: res.Data,
                    linkage: true,
                    linkageWidth: 100
                });
            }
        });
        formSelects.on('selsupplier', function (id, vals, val, isAdd, isDisabled) {
            if (isAdd == true) {
                if (val.value.indexOf("/") > 0 && ((val.value.split('/')).length - 1) >= 2) {  //多级选择时,第一级无法选中  供应商前两级都无法选中
                    var value = val.value.split('/')[val.value.split('/').length - 1];
                    $("#supplier").val(value);  //实际选中效果为多个ID组合 ‘id1/id2/id3’,取最后一个
                } else {
                    isAdd = false;
                    return false;
                }
            } else {
                $("#supplier").val("");
            }
        });

        form.on('switch(switchTest)', function (data) {
            var field = form.val("app-content-list");
            field["ship"] = data.elem.checked;
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        });
        form.on('switch(switchTest2)', function (data) {
            var field = form.val("app-content-list");
            field["wpick"] = data.elem.checked;
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        });
        form.on('switch(switchTest3)', function (data) {
            var field = form.val("app-content-list");
            field["groupbuy"] = data.elem.checked;
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        });

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        })

        form.on('submit(LAY-app-contlist-delete)', function (data) {
            $("#key").val("");
            layui.formSelects.value('cateID', []);
            layui.formSelects.value('dMode', []);
            layui.formSelects.value('selsupplier', []);
            $("#show").val('');
            //$("#storeID").val('');
            layui.formSelects.value('storeID', []);
            //$("#supplier").val('');
            form.render('select');

            var field = data.field;
            field.key = "";
            field.show = "";
            field.cateID = "";
            field.dMode = "";
            field.storeID = "";
            field.supplier = "";
            field["Order"] = "";
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        });
    });

    layui.data.setActive = function () {

        layui.use(['form', 'admin', 'table'], function () {
            var $ = layui.$;
            var form = layui.form;
            var admin = layui.admin;
            var view = layui.view;
            var table = layui.table;
            var active = {
                del: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    //return layer.msg('已删除');
                    layer.confirm('确定删除吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/goodsMgr/BatchDelete' //实际使用请改成服务端真实接口
                          , data: { "ids": ids }
                              , type: "post"
                            //, success: function (res) {
                            //}
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layer.msg(res.ResultInfo);
                              table.reload('LAY-app-content-list');
                          }
                        });
                    });
                },
                //添加
                add: function (othis) {
                    //location.hash = '/shop/shop_goods/addChild';
                    admin.popup({
                        title: '添加信息'
                      , skin: 'layui-layer-lan'
                  , area: ['80%', '96%']
                  , zIndex: 1002
                      , id: 'LAY-popup-content-add'
                      , success: function (layero, index) {
                          view(this.id).render('/shop/shop_goods/add').done(function () {
                              form.render(null, 'layuiadmin-app-form-list');
                              if (field.Images == "" || field.Images == null || field.Images == "[]") {
                                  layer.msg("请上传商品介绍图！");
                                  return false;
                              }
                              if (field.IsShipping_Sel == 0 && field.DeliveMode.indexOf("1") >= 0 && (field.FreightID == "" || field.FreightID == null)) {
                                  layer.msg("请选择运费模板！");
                                  return false;
                              }
                              field["MemberPrice"] = GetMemberPrice();

                              //监听提交
                              form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                  var field = data.field; //获取提交的字段
                                  //提交 Ajax 成功后，关闭当前弹层并重载表格
                                  //请求登入接口
                                  admin.req({
                                      url: layui.setter.APIUrl + '/shop/goodsMgr/Add' //实际使用请改成服务端真实接口
                                    , data: field
                                    , type: "post"
                                    , done: function (res) {
                                        layer.close(index); //执行关闭
                                        layui.table.reload('LAY-app-content-list'); //重载表格
                                        layer.msg('新增成功');
                                    }
                                  });
                              });
                          });
                      }
                    });
                },
                //设置网点自提
                setpick: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    var tip = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                        debugger;
                        if (eobj.DeliveMode.indexOf("0") < 0) {
                            tip = "请选择支持自提的商品";
                        } else if (eobj.IsStorePick_Sel == 1) {
                            tip = "选中商品已支持网点自提，请勿勾选";
                        }
                    });
                    if (tip != "") {
                        return layer.msg(tip);
                    }
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    layer.confirm('确定设置选中商品支持网点自提吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/goodsMgr/SetPick' //实际使用请改成服务端真实接口
                          , data: { "ids": ids, "pick": 1 }
                              , type: "post"
                            //, success: function (res) {
                            //}
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layer.msg("设置成功");
                              table.reload('LAY-app-content-list');
                          }
                        });
                    });
                },
                //取消网点自提
                cancelpick: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    var tip = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                        if (eobj.IsStorePick_Sel == 0) {
                            tip = "请选择支持网点自提的商品";
                        }
                    });
                    if (tip != "") {
                        return layer.msg(tip);
                    }
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    layer.confirm('确定设置选中商品支持网点自提吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/goodsMgr/SetPick' //实际使用请改成服务端真实接口
                          , data: { "ids": ids, "pick": 0 }
                              , type: "post"
                            //, success: function (res) {
                            //}
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layer.msg("取消成功");
                              table.reload('LAY-app-content-list');
                          }
                        });
                    });
                },
                //设置库存
                setstock: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);

                    //prompt层
                    layer.prompt({ title: '请输入库存数量', formType: 0 }, function (text, index) {
                        if (text == "" || text == null) {
                            layer.msg("请输入库存数量");
                            return false;
                        }
                        if (!(/(^[1-9]\d*$)/.test(text)) && text != 0) {
                            layer.msg("请输入正整数");
                            return
                        }
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/goodsMgr/SetStock' //实际使用请改成服务端真实接口
                          , data: { "ids": ids, "qty": text }
                              , type: "post"
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layer.msg("设置成功");
                              table.reload('LAY-app-content-list');
                          }
                        });

                        layer.close(index);
                    });
                },
                //上架
                showon: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    var tip = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                        if (eobj.IsShow_Sel == 0) {
                            tip = "选择商品当前状态为上架，请勿勾选";
                        }
                    });
                    if (tip != "") {
                        return layer.msg(tip);
                    }
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    layer.confirm('确定上架吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/goodsMgr/GoodShow' //实际使用请改成服务端真实接口
                          , data: { "ids": ids, "show": 0 }
                              , type: "post"
                            //, success: function (res) {
                            //}
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layer.msg("上架成功");
                              table.reload('LAY-app-content-list');
                          }
                        });
                    });
                },
                //下架
                showup: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    var tip = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                        if (eobj.IsShow_Sel == 1) {
                            tip = "选择商品当前状态为下架，请勿勾选";
                        }
                    });
                    if (tip != "") {
                        return layer.msg(tip);
                    }
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    layer.confirm('确定下架吗？下架后商城中将不可见。', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/goodsMgr/GoodShow' //实际使用请改成服务端真实接口
                          , data: { "ids": ids, "show": 1 }
                              , type: "post"
                            //, success: function (res) {
                            //}
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layer.msg("下架成功");
                              table.reload('LAY-app-content-list');
                          }
                        });
                    });
                },
                //团购
                groupbuy: function (othis) {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    checkids = [];
                    var tip = "";
                    $.each(checkData, function (index, eobj) {
                        if (eobj.IsShow_Sel == 1) {
                            tip = "未上架的商品不可加入团购";
                        } else if (eobj.GroupBuyInfo != null) {
                            tip = "选中商品正在团购时效内，无法再次加入团购";
                        } else {
                            checkids.push(eobj.PID);
                        }
                    });
                    if (tip != "") {
                        checkids = [];
                        return layer.msg(tip);
                    }

                    admin.popup({
                        title: '加入团购'
                      , skin: 'layui-layer-lan'
                      , area: ['60%', '75%']
                      , id: 'LAY-popup-content-add'
                      , success: function (layero, index) {
                          view(this.id).render('/shop/shop_groupbuy_goods/groupbuy').done(function () {
                              form.render(null, 'layuiadmin-app-form-list');

                              //监听提交
                              form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                  var field = data.field; //获取提交的字段
                                  //提交 Ajax 成功后，关闭当前弹层并重载表格
                                  //请求登入接口
                                  admin.req({
                                      url: layui.setter.APIUrl + '/shop/Groupbuy_goodsMgr/AddGoods' //实际使用请改成服务端真实接口
                                    , data: field
                                    , type: "post"
                                    , done: function (res) {
                                        layer.close(index); //执行关闭
                                        layui.table.reload('LAY-app-content-list'); //重载表格
                                        layer.msg('新增成功');
                                    }
                                  });
                              });
                          });
                      }
                    });
                },
                //设置购买限制
                setlimit: function (othis) {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    checkids = [];
                    $.each(checkData, function (index, eobj) {
                        checkids.push(eobj.PID);
                    });
                    admin.popup({
                        title: '设置购买限制'
                      , skin: 'layui-layer-lan'
                      , area: ['650px', '500px']
                      , zIndex: 1002
                      , id: 'LAY-popup-content-add'
                      , success: function (layero, index) {
                          view(this.id).render('/shop/shop_goods/SetLimit').done(function () {
                              form.render(null, 'layuiadmin-app-form-list');

                          });
                      }
                    });
                }
            };

            $('.layui-btn.layuiadmin-btn-list').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    }


    function orders(xh) {
        layui.use(['form', 'admin', 'table'], function () {
            var $ = layui.$;
            var form = layui.form;
            var admin = layui.admin;
            var table = layui.table;

            var field = form.val("app-content-list");
            field["Order"] = xh;
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        });
    }


</script>
